import {isEmpty, real} from '@/utils/CommonUtils';

export default function ({refTree, selectChange}) {
  function checkChange(data) {
    let node = refTree.value.getCheckedNodes();
    if (isEmpty(node)) {
      selectChange(undefined);
    } else {
      data = real(data);
      node = real(node[0]);
      refTree.value.setCheckedKeys([data.id]);
      selectChange(node);
    }
  }

  function nodeClick(node) {
    if (isEmpty(node)) {
      refTree.value.setCheckedKeys([]);
      return;
    }
    node = real(node);
    refTree.value.setCheckedKeys([node.id]);
    selectChange(node);
  }

  return {
    checkChange,
    nodeClick
  };
};

